const webpack = require('webpack')

const main = require('./webpack.main')
const renderer = require('./webpack.renderer')

/**
 * 打包     
 * @param {配置} config 
 * @return Promise 
 */
function package(config) {
    return new Promise((resolve, reject) => {
        config.mode = 'production'
        webpack(config, (error, stats) => {
            if (error) reject(error.stack || error)
            if (stats.hasErrors()) {
                let err = ''
                stats.toString({ chunks: false, colors: true }).split(/\r?\n/).forEach((line) => err += `   ${line}\n`)
                reject(err)
            }
            resolve(stats.toString({ chunks: false, colors: true }))
        })
    })
}

function run() {
    package(main)
        .then((r) => {
            console.log('[package] main: ', r);
        })
        .catch((err) => {
            console.error(`\n${err}\n`)
            process.exit(1)
        })
    package(renderer)
        .then((r) => {
            console.log('[package] renderer: ', r);
        })
        .catch((err) => {
            console.error(`\n${err}\n`)
            process.exit(1)
        })
}

run()